#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import os.path

from onvif_ptz.onvif_base import Onvif
from onvif_ptz.path_conf import BASE_DIR


def camera_absolute_move():
    onvif = Onvif()
    with open(os.path.join(BASE_DIR, 'input_ptz.json'), encoding='utf-8') as f:
        ptz_info = json.load(f)
    p = float(ptz_info['p'])
    t = float(ptz_info['t'])
    z = float(ptz_info['z'])
    try:
        ptz_type = onvif.ptz_service.create_type('AbsoluteMove')
        ptz_type.ProfileToken = onvif.media_service.GetProfiles()[0].token
        ptz_type.Position = {'PanTilt': {'x': p, 'y': t}, 'Zoom': z}
        ptz_type.Speed = {'PanTilt': {'x': 1, 'y': 1}, 'Zoom': 1}
        onvif.ptz_service.AbsoluteMove(ptz_type)
    except Exception as e:
        raise RuntimeError(f'相机绝对移动失败：{e}')
    else:
        print('相机绝对移动完成')
